package com.hnzbxy.api.biz.controller;

import com.hnzbxy.api.biz.entity.ClassInfo;
import com.hnzbxy.api.biz.service.ClassInfoService;
import com.hnzbxy.api.biz.vo.ClassInfoVO;
import com.hnzbxy.api.common.annotation.ControllerEndpoint;
import com.hnzbxy.api.common.bean.ResponseBean;
import com.hnzbxy.api.system.vo.PageVO;
import com.wuwenze.poi.ExcelKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 班级信息Controller（替换原ClassController，路径改为/classInfo）
 */
@Api(tags = "班级信息管理接口")
@RequestMapping("/classInfo") // 新路径：/classInfo（避免原/class冲突）
@RestController
@CrossOrigin
@Validated
public class ClassInfoController {

    @Autowired
    private ClassInfoService classInfoService;

    /**
     * 班级列表（分页+条件查询）
     */
    @ApiOperation(value = "班级列表", notes = "分页查询班级，支持名称/年级/状态/是否合班筛选")
    @RequiresPermissions({"classInfo:list"}) // 新权限标识：classInfo:list
    @GetMapping("/list")
    public ResponseBean list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize,
                             ClassInfoVO classInfoVO) {
        PageVO<ClassInfoVO> pageVO = classInfoService.findClassInfoList(pageNum, pageSize, classInfoVO);
        return ResponseBean.success(pageVO);
    }

    /**
     * 新增班级
     */
    @ControllerEndpoint(exceptionMessage = "新增班级失败", operation = "新增班级")
    @ApiOperation(value = "新增班级", notes = "传入班级信息（名称/年级/状态/是否合班必传）")
    @RequiresPermissions({"classInfo:add"}) // 新权限标识
    @PostMapping("/add")
    public ResponseBean add(@RequestBody @Validated ClassInfoVO classInfoVO) {
        ClassInfo classInfo = classInfoService.add(classInfoVO);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("id", classInfo.getId());
        resultMap.put("className", classInfo.getClassName());
        return ResponseBean.success(resultMap);
    }

    /**
     * 删除班级
     */
    @ControllerEndpoint(exceptionMessage = "删除班级失败", operation = "删除班级")
    @ApiOperation(value = "删除班级", notes = "根据ID删除指定班级")
    @RequiresPermissions({"classInfo:delete"}) // 新权限标识
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id) {
        classInfoService.delete(id);
        return ResponseBean.success();
    }

    /**
     * 班级详情（编辑用）
     */
    @ApiOperation(value = "班级详情", notes = "根据ID获取班级信息，用于编辑回显")
    @RequiresPermissions({"classInfo:edit"}) // 新权限标识
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id) {
        ClassInfoVO classInfoVO = classInfoService.edit(id);
        return ResponseBean.success(classInfoVO);
    }

    /**
     * 更新班级信息
     */
    @ControllerEndpoint(exceptionMessage = "更新班级失败", operation = "更新班级")
    @ApiOperation(value = "更新班级", notes = "根据ID更新班级信息（名称/年级/状态必传）")
    @RequiresPermissions({"classInfo:update"}) // 新权限标识
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id,
                               @RequestBody @Validated ClassInfoVO classInfoVO) {
        classInfoService.update(id, classInfoVO);
        return ResponseBean.success();
    }

    /**
     * 导出班级Excel
     */
    @ApiOperation(value = "导出班级Excel", notes = "导出筛选条件下的班级信息到Excel")
    @RequiresPermissions({"classInfo:export"}) // 新权限标识
    @PostMapping("/exportExcel")
    @ControllerEndpoint(exceptionMessage = "导出班级Excel失败", operation = "导出班级Excel")
    public void exportExcel(HttpServletResponse response, ClassInfoVO classInfoVO) {
        List<ClassInfo> classInfoList = classInfoService.findAllByCondition(classInfoVO);
        ExcelKit.$Export(ClassInfo.class, response).downXlsx(classInfoList, false);
    }
}